﻿Imports System.Net
Imports System
Imports ComponentAce.Compression.ZipForge
Imports ComponentAce.Compression.Archiver
Public Class Form1
    Public Shared WithEvents httpclient As WebClient
    Public Shared WithEvents archiver As ZipForge
    Dim WithEvents WC As New WebClient
    Dim appdata As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (Not System.IO.Directory.Exists(appdata & "\ACR")) Then
            System.IO.Directory.CreateDirectory(appdata & "\ACR")
        End If
        CheckForUpdates()
        If (Not System.IO.File.Exists(appdata & "\ACR\ZipForge.dll")) Then
            zipforge()
        End If
        If (Not System.IO.Directory.Exists(appdata & "\ACR\data")) Then
            Button5.Visible = True
            Label1.Visible = True
            Label2.Visible = True
        End If
    End Sub
    Public Sub CheckForUpdates()
        'Checks for updates for the data'
        Try
            Dim urlfile As String = Application.StartupPath & "url.dat"
            Dim datafile As String = Application.StartupPath & "data.dat"
            Dim MyVer As String = My.Computer.FileSystem.ReadAllText(appdata & "\ACR\versions\data.dat")

            If My.Computer.FileSystem.FileExists(datafile) Then
                My.Computer.FileSystem.DeleteFile(datafile)
            End If

            My.Computer.Network.DownloadFile("example.com/data.dat", datafile)
            My.Computer.Network.DownloadFile("example.com/url.dat", urlfile)
            Dim LastVer As String = My.Computer.FileSystem.ReadAllText(datafile)

            If Not MyVer = LastVer Then
                MsgBox("Client Update Available")
                Button1.Visible = True
                Label1.Visible = True
                Label2.Visible = True
            End If
        Catch ae As ArchiverException
        End Try
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Visible = False
        download_data()
    End Sub
    Private Sub WC_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
    End Sub
    Private Sub unzip_data()
        Dim archiver As New ZipForge()
        Try
            ' The name of the ZIP file to unzip
            archiver.FileName = appdata & "\ACR\data.zip"
            ' Open an existing archive
            archiver.OpenArchive(System.IO.FileMode.Open)
            ' Default path for all operations                
            archiver.BaseDir = appdata & "\ACR\"
            ' Extract all files from the archive to C:\Temp folder
            archiver.ExtractFiles("*.*")
            ' Close archive
            archiver.CloseArchive()
            ' Catch all exceptions of the ArchiverException type
        Catch ae As ArchiverException
            Console.WriteLine("Message: {0} Error code: {1}", ae.Message, ae.ErrorCode)
            ' Wait for keypress
            Console.ReadLine()
        End Try
        Do While archiver.Active
            Application.DoEvents()
        Loop
        My.Computer.FileSystem.DeleteFile(appdata & "\ACR\data.zip")
        My.Computer.FileSystem.DeleteFile(appdata & "\ACR\url.dat")
        My.Computer.Network.DownloadFile("example.com/Client.bat", appdata & "\ACR\Client.bat")
        My.Computer.Network.DownloadFile("example.com/Server.bat", appdata & "\ACR\Server.bat")
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            Label2.Text = "Status: Done Downloading."
        End If
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            Timer1.Stop()
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim p As New System.Diagnostics.Process()
        p.StartInfo.FileName = appdata & "\ACR\Client.bat"
        p.Start()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim p As New System.Diagnostics.Process()
        p.StartInfo.FileName = appdata & "\ACR\Server.bat"
        p.Start()
    End Sub
    Private Sub download_data()
        Try
            Dim url As String = My.Computer.FileSystem.ReadAllText(appdata & "\ACR\url.dat")
            My.Computer.FileSystem.DeleteFile(appdata & "\ACR\versions\data.dat")
            Label2.Text = "Status: Downloading Client..."
            Timer1.Start()
            ProgressBar1.Value = ProgressBar1.Minimum
            ProgressBar1.Visible = True
            'Dim filepath As String = appdata "\ACR\versions\data.dat"'
            WC.DownloadFileAsync(New Uri(url), appdata & "\ACR\data.zip")
            My.Computer.Network.DownloadFile("example.com/data.dat", appdata & "\ACR\versions\data.dat")
            If ProgressBar1.Value = ProgressBar1.Minimum Then
                Button1.Visible = False
            End If
            Do While WC.IsBusy
                Application.DoEvents()
            Loop
            Button1.Visible = False
            unzip_data()
        Catch ae As ArchiverException
        End Try
    End Sub
    Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Button5.Visible = False
        install_once_data()
    End Sub
    Private Sub install_once_data()
        Try
            Dim url As String = My.Computer.FileSystem.ReadAllText(appdata & "\ACR\url.dat")
            My.Computer.FileSystem.DeleteFile(appdata & "\ACR\versions\data.dat")
            Label2.Text = "Status: Downloading Client..."
            Timer1.Start()
            ProgressBar1.Value = ProgressBar1.Minimum
            ProgressBar1.Visible = True
            'Dim filepath As String = appdata "\ACR\versions\data.dat"'
            WC.DownloadFileAsync(New Uri(Url), appdata & "\ACR\data.zip")
            My.Computer.Network.DownloadFile("example.com/data.dat", appdata & "\ACR\versions\data.dat")
            Do While WC.IsBusy
                Application.DoEvents()
            Loop
            unzip_data()
        Catch ae As ArchiverException
        End Try
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Form2.Show()
    End Sub
    Private Sub zipforge()

        My.Computer.Network.DownloadFile("example.com/ZipForge.dll", appdata & "\ACR\ZipForge.dll")
    End Sub
End Class
